<?php

use Illuminate\Support\Collection;
use Illuminate\View\View;

class MenuComposer {

    public function compose(View $view) {
        
//        if (Cache::has('menu_publico')) {
//            $view->menu = Cache::get('menu_publico');
//        }
//        else {
            $tagRelatos = TagRel::active()->get(['nombre', 'slug', 'id']);
            $tagsObjetos = TagObj::active()->get(['nombre', 'slug', 'id']);
            $apartment = Apartment::active()->orderBy('fecha', 'desc')->take(1)->get(['id', 'fecha'])->first();
            $about = Page::active()->take(1)->get()->first();
            
            $menu = new Collection;
            
            //Categorias de Relatos
            $sub1 = new Collection; 
            foreach ($tagRelatos as $tag_relato) { 
                $sub1->push((object) [
                        'title' => $tag_relato->nombre, 
                        'link' => URL::route('categoria_rel', ['slug' => $tag_relato->slug, 'id' => $tag_relato->id])
                    ]);
            }
            if(!$sub1->isEmpty()){
                $menu->push((object) ['title' => 'Relatos', 'menu' => $sub1]);
            }
            
            //Categorias de Objetos
            $sub1 = new Collection; 
            foreach ($tagsObjetos as $tag_objeto) {
                $sub1->push((object) [
                        'title' => $tag_objeto->nombre, 
                        'link' => URL::route('categoria_obj', ['slug' => $tag_objeto->slug, 'id' => $tag_objeto->id])
                    ]);
            }
            if (!$sub1->isEmpty()){
                $menu->push((object) ['title' => 'Objetos', 'menu' => $sub1]);
            }
            
            //////////////////
            // SIN SUBITEMS //
            //////////////////
            
            // The Apartment
            if( ! is_null( $apartment ) ){
                $route = URL::route('apartment', ['fecha' => $apartment->fecha->toDateString(), 'id' => $apartment->id]);
                $menu->push((object) ['link' => HTML::link($route, 'The Apartment')] );
            }
            
            // About
            if (! is_null($about)) {
                $route = URL::route('page', ['slug' => $about->slug, 'id' => $about->id]);
                $menu->push((object) ['link' => HTML::link($route, $about->titulo) ]);
            }
            
            // Newsletter
            $menu->push((object) ['link' => HTML::link('#', 'Newsletter', ['data-toggle' => 'modal', 'data-target' => '.bs-example-modal-sm']) ]);
            
            // Socials
            $menu->push((object) ['link' => '<a href="http://www.enjoygram.com/krethaus"><i class="fa fa-instagram fa-lg"></i></a>' ]);
            $menu->push((object) ['link' => '<a href="https://es-es.facebook.com/Krethaus"><i class="fa fa-facebook fa-lg"></i></a>' ]);
            
            // Guardo en cache en minutos
//            Cache::add('menu_publico', $menu, 10);
            
            // Paso coleccion a la vista
            $view->menu = $menu;            
//       }

    }
}
